//
//  CSGXHomeSaleCommitPreviewController.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/10/28.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXHomeSaleCommitPreviewController.h"
#import "CSGXBaseTableView.h"
#import "CSGXHomeSaleController.h"
#import "CSGXHomeSaleCategoryView.h"
#import "LKGXNumberModificationView.h"
#import "CSGXPreviewCell.h"
#import "CSGXHomeSalePreviewBalanceController.h"
#import "CSGXFootView.h"

#import "CSHomeSearchSaleController.h"

#import "TOrderInfoDAL.h"

static NSString * priviewCellId = @"priviewCellId";

@interface CSGXHomeSaleCommitPreviewController () <CSGXCategoryViewActionDelegate, UITableViewDataSource>


@property (nonatomic,weak) CSGXHomeSaleCategoryView * categoryView;

@property(nonatomic,weak) CSGXBaseTableView * tvDetail;

@property (nonatomic,weak) CSGXFootView * footView;

@property (nonatomic, strong) TOrderInfoModel *previewOrderModel;
@property (nonatomic, strong) NSArray<TSaleInfoModel *> * saleListPreview;


@end

@implementation CSGXHomeSaleCommitPreviewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self distribute];
}

- (void)viewWillDisappear:(BOOL)animated {
    self.isDashedModify = NO;
}

#pragma mark - methods
- (void)distribute {
    NSString *orderNo = [CSUserDefaultsTool getOrderNo]; //修改订单，取得本次已存在订单号
    self.previewOrderModel = [TOrderInfoDAL getOrderModelWithOrderNo:orderNo saleList:self.saleListPreview];
    self.footView.prevFooterOrderModel = _previewOrderModel;
    
    self.categoryView.bubbleDicts = @[@{@"bubbleNumber":@(self.saleListPreview.count), @"bubbleIndex":@(0), @"bubbleColor":[UIColor orangeColor]}];
}


#pragma mark - actions

- (void)btnLeftAction:(id)sender {
    
    if (self.isDashedModify) {
        // alertVc
        UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"放弃修改？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"放弃" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            [self.navigationController popToViewController:[[CSHomeSearchSaleController alloc] init] animated:YES];
        }]];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alertVc animated:YES completion:nil];
        
        return;
    }
    
    
    // alertVc
    UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"放弃编辑" message:@"确认放弃编辑退出？" preferredStyle:UIAlertControllerStyleAlert];
    [alertVc addAction:[UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //call back
        [self.navigationController popToRootViewControllerAnimated:YES];
    }]];
    [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alertVc animated:YES completion:nil];
}

- (void)btnEditAction:(UIBarButtonItem *)btnEdit {
//    NSLog(@"0:%@, \r\n1:%@, \r\n2:%@ \r\n", self.navigationController.viewControllers[0], self.navigationController.viewControllers[1], self.navigationController.viewControllers[2]);
    
    CSGXHomeSaleController * saleVc = self.navigationController.viewControllers[1];
    saleVc.isReceivedHome = YES;
    saleVc.saleModelList = self.saleListPreview;
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)btnScanAction:(UIBarButtonItem *)btnScan {
    
}

- (void)btnSearchAction:(UIBarButtonItem *)btnSearch {
    
}


#pragma mark - delegates
/// 点击了categoryView上的哪个按钮
- (void)gxCategoryView:(CSGXHomeSaleCategoryView *)categoryView withSelectedIndex:(NSInteger)selectedIdnex {
    
}


#pragma mark - tableView datasource and delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.saleListPreview.count;
}

-(CSGXPreviewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CSGXPreviewCell * cell = [tableView dequeueReusableCellWithIdentifier:priviewCellId];
    cell.salePreviewCellModel = self.saleListPreview[indexPath.row];
    cell.cellIndex = indexPath.row;
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(previewCellCallBackCountAction:) name:@"previewCellCallBackCount" object:cell];
    cell.callback_previewCell = ^{
        [self distribute];
    };
    return cell;
}


#pragma markl - setupUI
- (void)setNavBar {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"车销出库" style:UIBarButtonItemStylePlain target:self action:@selector(btnLeftAction:)];
    
    UIBarButtonItem * rightEdit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(btnEditAction:)];
    UIBarButtonItem * rightScan = [[UIBarButtonItem alloc] initWithTitle:@"扫描" style:UIBarButtonItemStylePlain target:self action:@selector(btnScanAction:)];
    UIBarButtonItem * rightSearch = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(btnSearchAction:)];
    
    self.navigationItem.rightBarButtonItems = @[rightEdit, rightScan, rightSearch];
}

- (void)setupUI {
    // navBar
    [self setNavBar];
    
    // CSGXHomeSaleCategoryView
    CSGXHomeSaleCategoryView * categoryView = [CSGXHomeSaleCategoryView gxCategoryViewWithDelegate:self page:1];
    [self.view addSubview:categoryView];
    
    [categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.leading.trailing.equalTo(self.view);
        make.height.offset(kHeightCategory * 2/3);
    }];
    
    categoryView.segments = @[@"销售", @"处理", @"还货", @"换货", @"退货"];
    
    
    // tvDetail
    CSGXBaseTableView * tvDetail = [[CSGXBaseTableView alloc] init];
    [self.view addSubview:tvDetail];
    tvDetail.backgroundColor = [UIColor clearColor];
    
    [tvDetail registerClass:[CSGXPreviewCell class] forCellReuseIdentifier:priviewCellId];
    tvDetail.dataSource = self;
    tvDetail.estimatedRowHeight = 100;
    tvDetail.rowHeight = UITableViewAutomaticDimension;
    
    [tvDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(categoryView.mas_bottom);
        make.leading.bottom.trailing.equalTo(self.view);
    }];
    
    // footView
    CSGXFootView * footView = [[[NSBundle mainBundle] loadNibNamed:@"CSGXFootView" owner:nil options:nil] lastObject];
    [self.view addSubview:footView];
    
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.offset(60);
    }];
    
#pragma mark call back from callBackBlock_FootView
    // call back from callBackBlock_FootView
    footView.callBackBlock_FootView = ^{
        //
        CSGXHomeSalePreviewBalanceController * balanceVc = [[CSGXHomeSalePreviewBalanceController alloc] init];
        balanceVc.prevBalanceDict = @{@"orderModel":self.previewOrderModel, @"saleList":self.saleListPreview};
        [self.navigationController pushViewController:balanceVc animated:YES];
    };
    
    //
    self.categoryView = categoryView;
    self.tvDetail = tvDetail;
    self.footView = footView;
}



#pragma mark - setter & getter
- (void)setDataPreviewDict:(NSDictionary *)dataPreviewDict {
    _dataPreviewDict = dataPreviewDict;
    
    self.previewOrderModel = dataPreviewDict[@"orderModel"];
    self.saleListPreview = dataPreviewDict[@"saleList"];
    
}



@end
